#include<stdio.h>
#include<stdlib.h>

struct Node
{
	struct Node* next;
	int val;
} *p, *head, *tail, *q, *t;

int main()
{
	//creat linked-list
	p = (struct Node*)malloc(sizeof(struct Node));
	head = p;
	tail = p;
	head -> next = NULL;
	
	int n;
	printf("Please input times:>");
	scanf("%d", &n);
	
	//initialize linked-list
	int i = 0;
	for(i = 0; i < n; i ++)
	{
		p = (struct Node*)malloc(sizeof(struct Node));
		
		printf("Please input the value:>");
		scanf("%d", &p -> val);
		
		tail -> next = p;
		tail = p;
		p -> next = NULL;
	}
	
	//add node to linked-list
	q = (struct Node*)malloc(sizeof(struct Node));
	printf("Please input a number you wanna add:>");
	scanf("%d", &q -> val);
	
	p = head;
	while(p -> next != NULL)
	{
		if(q -> val < p -> next -> val)
		{
			q -> next = p -> next;
			p ->next = q;
			break;
		}
		p = p -> next;
	}
	
	//delet node
	int num;
	printf("Now input a number you wanna delet:>");
	scanf("%d", &num);
	p = head -> next;
	while(p -> next != NULL && p -> next -> val != num)
	{
		p = p -> next;
	} 
	
	t = p -> next;
	p -> next = p -> next -> next;
	free(t);
	
	
	//print linked-list
	printf("Here is the Linked list :");
	p = head -> next;
	while(p != NULL)
	{
		printf("%d ", p->val);
		p = p -> next;
	}
	
	//free all memory
	p = head;
	while(p != NULL)
	{
		struct Node* temp = p;
		p = p -> next;
		free(temp);
	}
	
	return 0;
}
